package com.mjl.shops.view.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mjl.shops.R;
import com.mjl.shops.view.IconTextView;

public class GoodsDetailsBuyDialog {

    private View dialogView;
    private Dialog dialog;
    private Activity context;
    private BuyDialogListener buyDialogListener;
    private TextView mBuy, buy_num_tv;
    private RelativeLayout total_rl;
    private IconTextView add_buy_num, reduce_buy_num;
    private int buyNum = 1;
    private int repertoryNum = 2564;

    public void setBuyDialogListener(BuyDialogListener buyDialogListener) {
        this.buyDialogListener = buyDialogListener;
    }

    public GoodsDetailsBuyDialog(Activity contexts) {
        this.context = contexts;
        if (context == null) {
            return;
        } else {
            initView();
        }
    }

    //初始化dialog
    private void initView() {
        dialogView = LayoutInflater.from(context).inflate(R.layout.dialog_goods_deteils_buy_layout, null, false);
        dialog = new Dialog(context, R.style.AlertDialogStyle);
        mBuy = dialogView.findViewById(R.id.buy_tv);
        total_rl = dialogView.findViewById(R.id.total_rl);
        add_buy_num = dialogView.findViewById(R.id.add_buy_num);
        reduce_buy_num = dialogView.findViewById(R.id.reduce_buy_num);
        buy_num_tv = dialogView.findViewById(R.id.buy_num_tv);
        reduce_buy_num.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (buyNum > 1) {
                    buyNum--;
                    buy_num_tv.setText("" + buyNum);
                } else {
                    buyNum = 1;
                    buy_num_tv.setText("" + buyNum);
                }
            }
        });
        add_buy_num.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (buyNum >= repertoryNum) {
                    buyNum = repertoryNum;
                    buy_num_tv.setText("" + buyNum);
                } else {
                    buyNum++;
                    buy_num_tv.setText("" + buyNum);
                }
            }
        });
        mBuy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (buyDialogListener != null) {
                    buyDialogListener.buy();
                }
            }
        });

        total_rl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ShutDown();
                if (buyDialogListener != null) {
                    buyDialogListener.shutDown();
                }
            }
        });

        Window window = dialog.getWindow();
        //为了消除dialog的默认padding
        window.getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams wl = window.getAttributes();
        wl.x = context.getWindowManager().getDefaultDisplay().getWidth();
        wl.y = context.getWindowManager().getDefaultDisplay().getHeight();

        wl.width = ViewGroup.LayoutParams.MATCH_PARENT;
        wl.height = ViewGroup.LayoutParams.MATCH_PARENT;
        // 设置显示位置
        dialog.onWindowAttributesChanged(wl);
        dialog.setContentView(dialogView);
    }

    public void Shows() {
        if (dialog != null && !dialog.isShowing()) {
            dialog.show();
        }
    }

    public void ShutDown() {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }

    public boolean getIsShow() {
        if (dialog != null && dialog.isShowing()) {
            return true;
        } else {
            return false;
        }
    }

    public void release() {
        if (context != null) {
            context = null;
        }
    }

    public interface BuyDialogListener {
        void buy();

        void shutDown();
    }
}
